{extend name="public/base" /}
{block name="button"}
<a href='{:url("add")}'  class='layui-btn layui-btn-sm layui-btn-primary'>添加角色</a>
{/block}
{block name="content"}
<form class="layui-form layui-form-pane form-search" action="{:url()}" onsubmit="return false" method="post" autocomplete="off">
    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">角色名称</label>
        <label class="layui-input-inline">
            <input name="name" value="" placeholder="" class="layui-input">
        </label>
    </div>
    <div class="layui-form-item layui-inline">
        <button class="layui-btn layui-btn-primary"><i class="layui-icon">&#xe615;</i>搜索</button>
    </div>
</form>
<table id="RoleTable" data-url="{:url()}" data-target-search="form.form-search"></table>
{/block}

{block name='script'}
<script>
    $(function () {
        // 初始化表格组件
        $('#RoleTable').layTable({
            even: true,
            sort: {field: 'id', type: 'desc'},
            height: 'full',
            method: 'post',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'name', title: '角色名称', align: 'center', minWidth: 140},
                {field: 'desc', title: '角色描述', align: 'center', minWidth: 110, templet: '<div>{{d.desc||"-"}}</div>'},
                {field: 'status', title: '状态', align: 'center', minWidth: 110, templet: '#StatusSwitchRoleTableTpl'},
                {field: 'create_operator', title: '创建人账户', align: 'center', minWidth: 170, sort: true},
                {field: 'update_operator', title: '更新人账户', align: 'center', minWidth: 170, sort: true},
                {toolbar: '#ToolbarRoleTableTpl', title: '操作面板', align: 'center', minWidth: 210, fixed: 'right'},
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitchRoleTable)', function (obj) {
            let data = {id: obj.value, status: obj.elem.checked > 0 ? 1 : -1};
            $.form.load("{:url('status')}", data, 'post', function (ret) {
                if (ret.code >0) $.msg.error(ret.msg, 3, function () {
                    $('#RoleTable').trigger('reload');
                });
                return false;
            }, false);
        });
        $.menu.highlight_subnav("{:url('index')}")
    });

</script>
<script type="text/html" id="StatusSwitchRoleTableTpl">
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="已激活|已禁用" lay-filter="StatusSwitchRoleTable" {{-d.status==1 ?'checked':''}}>
    {{-d.status ? '<b class="color-green">已启用</b>' : '<b class="color-red">已禁用</b>'}}
</script>

<!-- 数据操作工具条模板 -->
<script type="text/html" id="ToolbarRoleTableTpl">
    <a class="layui-btn layui-btn-primary layui-btn-sm" href='{:url("edit")}?id={{d.id}}'>编 辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" data-action="{:url('remove')}" data-value="id#{{d.id}}" data-confirm="确定要删除角色吗?">删 除</a>
</script>
{/block}